import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_utils/screen.dart';

//中间文字的appBar
class CustomAppbar extends StatefulWidget implements PreferredSizeWidget {
  final Widget? leading;
  final Widget? backImage;
  final String title;
  final List<Widget>? actions;
  final VoidCallback? backClick;
  final bool hideBackBtn;
  final Widget? child;
  final Color? bgColor;
  final Color? backColor;
  final Color? lineColor;
  final Color? titleColor;

  CustomAppbar({
    this.leading,
    this.title = '',
    this.actions,
    this.backClick,
    this.hideBackBtn = false,
    this.child,
    this.bgColor,
    this.backColor,
    this.backImage,
    this.lineColor,
    this.titleColor = const Color(0xffFFFFFF),
  }) : super();

  @override
  State<StatefulWidget> createState() {
    return _CustomAppbarState();
  }

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight + screen.paddingTop);
}

class _CustomAppbarState extends State<CustomAppbar> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        height: kToolbarHeight + screen.paddingTop,
        padding: EdgeInsets.only(
          top: screen.paddingTop,
          right: 16,
          left: 16,
        ),
        decoration: BoxDecoration(color: widget.bgColor ?? Colors.transparent, boxShadow: const [
          //阴影
          BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.03), offset: Offset(0.0, 4.0), blurRadius: 9.0)
        ]),
        child: Stack(
          alignment: Alignment.center,
          children: <Widget>[
            Container(
              child: widget.child ??
                  Text(
                    widget.title,
                    style: TextStyle(
                      fontSize: 18,
                      color: widget.titleColor,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
            ),
            Positioned(
              left: 0,
              child: widget.leading ??
                  (widget.hideBackBtn
                      ? Container()
                      : GestureDetector(
                          child: Container(
                              width: kToolbarHeight,
                              height: kToolbarHeight,
                              color: Colors.transparent,
                              alignment: Alignment.centerLeft,
                              child: widget.backImage ??
                                  Image.asset(
                                    'common_back.png'.commonImgPath,
                                    width: 24,
                                    height: 24,
                                    color: widget.backColor,
                                  )),
                          onTap: () {
                            if (widget.backClick != null) {
                              widget.backClick!();
                            } else {
                              if (Navigator.canPop(context)) {
                                Get.back();
                              }
                            }
                          },
                        )),
            ),
            Positioned(
              right: 0,
              child: Row(children: widget.actions ?? []),
            ),
            Positioned(bottom: 0, right: 0, left: 0, child: Container(height: 1, color: widget.lineColor ?? const Color(0x06000000)))
          ],
        ));
  }
}
